﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Data.OleDb;
using System.IO;
using System.Data.SqlClient;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using System.Configuration;
using System.Dynamic;

namespace WebApplication1
{
    /// <summary>
    /// Handler1 的摘要说明
    /// </summary>
    public class getList : IHttpHandler
    {
        public static readonly string bz = ConfigurationManager.ConnectionStrings["conn1"].ConnectionString;

        public void ProcessRequest(HttpContext context)
        {
            string authHeader = HttpContext.Current.Request.Headers["Authorization"];
            string bearerToken = authHeader.Substring("Bearer ".Length).Trim();
            PetaPoco.Database dbw = new PetaPoco.Database(bz);

        
            object jsonData = "";
            FileLogHelper.WriteTxtLog("调用开始", "11");
            dynamic jsonF = new ExpandoObject();  //动态MODEL 存DATE
            dynamic res = new ExpandoObject();  //动态MODEL 返回值

            TokenModel model = dbw.FirstOrDefault<TokenModel>("select  * from  Token  where token=@token and  DATEDIFF(DAY,date, GETDATE())=0", new { token = bearerToken });
            if (model == null)
            {
                res.code = 401;
                res.msg = "密钥不合法或者已经过期";
                res.data = "XXXX";
                res.count = 0;
                jsonData = JsonConvert.SerializeObject(res);
            }
            else
            {
                try
                {
                    byte[] postData = new byte[context.Request.InputStream.Length];
                    context.Request.InputStream.Read(postData, 0, postData.Length);
                    string postDataString = System.Text.Encoding.UTF8.GetString(postData);
                    FileLogHelper.WriteTxtLog("调用开始", postDataString);
                    U8faModel U8fa = JsonConvert.DeserializeObject<U8faModel>(postDataString);
                    int flag = 0;


                    #region 核心View_Customer
                    if (U8fa.tableView == "Customer")
                    {
                        FileLogHelper.WriteTxtLog("调用开始eeeee", U8fa.allField.ToString());
                        flag = 1;
                        if (U8fa.condition != "")
                        {
                            U8fa.condition = "where  " + U8fa.condition;
                        }


                        List<CustomerModel> model1 = dbw.Fetch<CustomerModel>("select  " + "*" + " from  " + "CUSTOMER left join   tc_type  on tc_type.type_code=CUSTOMER.cCusMngTypeCode  " + "    " + U8fa.condition + "  ");
                        // 转换为结果对象
                        if (model1.Count != 0)
                        {
                            jsonData = JsonConvert.SerializeObject(model1);
                            res.msg = "";
                            res.data = jsonData;
                            res.count = model1.Count;
                        }
                        else
                        {
                            jsonData = "";
                            res.msg = "";
                            res.data = jsonData;
                            res.count = 0;
                        }
                    }
                    #endregion



                    #region 核心XZ_SADOC
                    if (U8fa.tableView == "XZ_SADOC")
                    {
                        FileLogHelper.WriteTxtLog("调用开始eeeee", U8fa.allField.ToString());
                        flag = 1;
                        if (U8fa.condition != "")
                        {
                            U8fa.condition = "where  " + U8fa.condition;
                        }
                        List<dynamic> model1 = dbw.Fetch<dynamic>("select  " + "*" + " from  " + " XZ_SADOC  " + "    " + U8fa.condition + "  ");
                        // 转换为结果对象
                        // 转换为结果对象
                        if (model1.Count != 0)
                        {
                            jsonData = JsonConvert.SerializeObject(model1);
                            res.msg = "";
                            res.data = jsonData;
                            res.count = model1.Count;
                        }
                        else
                        {
                            jsonData = "";
                            res.msg = "";
                            res.data = jsonData;
                            res.count = 0;
                        }
                    }
                    #endregion



                    #region 核心XZ_SADOC
                    if (U8fa.tableView == "XZ_work")
                    {
                        FileLogHelper.WriteTxtLog("调用开始eeeee", U8fa.allField.ToString());
                        flag = 1;
                        if (U8fa.condition != "")
                        {
                            U8fa.condition = "where  " + U8fa.condition;
                        }
                        List<dynamic> model1 = dbw.Fetch<dynamic>("select  " + "*" + " from  " + " XZ_work  " + "    " + U8fa.condition + "  ");
                        // 转换为结果对象
                        // 转换为结果对象
                        if (model1.Count != 0)
                        {
                            jsonData = JsonConvert.SerializeObject(model1);
                            res.msg = "";
                            res.data = jsonData;
                            res.count = model1.Count;
                        }
                        else
                        {
                            jsonData = "";
                            res.msg = "";
                            res.data = jsonData;
                            res.count = 0;
                        }
                    }
                    #endregion


                    if (flag == 0)
                    {
                        res.code = 999;
                        res.msg = "视图不合法";
                        res.data = "";
                        res.count = 0;
                        jsonData = JsonConvert.SerializeObject(res);
                    }
                    else
                    {

                        res.data = jsonData;
                        jsonData = JsonConvert.SerializeObject(res);

                    }






                }

                catch (Exception ee)
                {
                    if (ee.Message.Length > 50)
                    {
                        res.msg = ee.Message.Substring(0, 50);
                    }
                    else
                    {
                        res.msg = ee.Message;
                    }
                    res.code = 999;
                    res.data = "{}";
                    res.count = 0;
                    jsonData = JsonConvert.SerializeObject(res);
                    context.Response.ContentType = "application/json";
                    context.Response.Write(jsonData);
                    return;
                }
            }


            // 写入响应
            FileLogHelper.WriteTxtLog("调用开始dddd", jsonData.ToString());
         //   string resstr = jsonData;
            ;
       //     context.Response.ContentType = "application/json";
            context.Response.Write(jsonData);
        }




        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        public string res(string ccode, string msg, string jsonData)
        {
            string resstr = "";


            return resstr;
        }


    }
}